package com.hucii.app.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.hucii.app.R;
import com.hucii.app.adapter.GridViewAdapter;
import com.hucii.app.adapter.GridViewPicAdapter;
import com.hucii.app.adapter.GridViewPicAdapter.TableCell;
import com.hucii.app.adapter.GridViewPicAdapter.TableRow;
import com.hucii.app.entity.GridViewItemInfo;
import com.hucii.app.entity.SearchEntities;

public class PicViewActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener, OnItemSelectedListener, TextWatcher {

	private EditText et_keyword;
	private Button btn_search;
	private Spinner sp_price_type;
	private Spinner sp_order_type;
	private RadioGroup radioGroup;
	private RadioButton rb_underwear;
	private RadioButton rb_brassiere;
	private RadioButton rb_briefs;
	private TextView tv_btn_thumbnail;
	private TextView tv_btn_list;
	private ListView lv_order_detail;
	private GridView gv_order_detail;
	private GridViewAdapter gridViewAdapter;
	private GridViewPicAdapter gridViewPicAdapter;

	private Map<Integer, Object> mMap;
	private ArrayAdapter spPriceTypeAdapter;
	private ArrayAdapter spOrderTypeAdapter;

	private ArrayList<TableRow> table = new ArrayList<TableRow>();
	private TableCell[] cells = new TableCell[4];// 每行4个单元
	private SearchEntities searchEntities;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setView(R.layout.main_ui);

		initData();
		initView();
		registerListener();
		display();

	}

	private void initData() {
		searchEntities = new SearchEntities();

		mMap = new HashMap<Integer, Object>();
		for (int i = 0; i < 100; i++) {
			GridViewItemInfo gridViewItemInfo = new GridViewItemInfo();
			gridViewItemInfo.setStyleId("77777");
			gridViewItemInfo.setOrderNums("77");
			gridViewItemInfo.setType("木代尔三角");
			mMap.put(i, gridViewItemInfo);
		}

		int width = this.getWindowManager().getDefaultDisplay().getWidth()
				/ cells.length;
		// 每行的数据
		for (int i = 0; i < cells.length; i++) {
			cells[i] = new TableCell("No." + String.valueOf(i), width + 8 * i,
					90, TableCell.STRING);
			cells[i].mStyleId = "00002";
			cells[i].mOrderNums = "2";
			cells[i].mType = "木代尔三角";
		}
		// 把表格的行添加到表格
		for (int i = 0; i < 12; i++)
			table.add(new TableRow(cells));
	}

	private void initView() {
		et_keyword = (EditText) findViewById(R.id.et_keyword);
		btn_search = (Button) findViewById(R.id.btn_search);
		sp_price_type = (Spinner) findViewById(R.id.sp_price_type);
		sp_order_type = (Spinner) findViewById(R.id.sp_order_type);
		radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
		rb_underwear = (RadioButton) findViewById(R.id.rb_underwear);
		rb_brassiere = (RadioButton) findViewById(R.id.rb_brassiere);
		rb_briefs = (RadioButton) findViewById(R.id.rb_briefs);
		tv_btn_list = (TextView) findViewById(R.id.tv_btn_list);
		tv_btn_thumbnail = (TextView) findViewById(R.id.tv_btn_thumbnail);
		lv_order_detail = (ListView) findViewById(R.id.lv_order_detail);
		gv_order_detail = (GridView) findViewById(R.id.gv_order_detail);
	}

	private void registerListener() {
		btn_search.setOnClickListener(this);

		getBaseLayout().getTv_header_left().setOnClickListener(this);
		getBaseLayout().getTv_personal_data().setOnClickListener(this);
		getBaseLayout().getTv_message().setOnClickListener(this);
		getBaseLayout().getTv_exit().setOnClickListener(this);

		et_keyword.addTextChangedListener(this);
		sp_order_type.setOnItemSelectedListener(this);
		sp_price_type.setOnItemSelectedListener(this);
		radioGroup.setOnCheckedChangeListener(listener);

		tv_btn_list.setOnClickListener(this);
		tv_btn_thumbnail.setOnClickListener(this);
		gv_order_detail.setOnItemClickListener(this);

	}

	private void display() {
		setTitle(" ", "", " ");

		// 纯文字的适配器
		gridViewAdapter = new GridViewAdapter(this, mMap);
		// 含图片的适配器
		gridViewPicAdapter = new GridViewPicAdapter(this, table);

		lv_order_detail.setAdapter(gridViewPicAdapter);
		gv_order_detail.setAdapter(gridViewAdapter);

		if (tv_btn_list.isEnabled()) {
			lv_order_detail.setVisibility(View.GONE);
			gv_order_detail.setVisibility(View.VISIBLE);
		} else if (tv_btn_thumbnail.isEnabled()) {
			lv_order_detail.setVisibility(View.VISIBLE);
			gv_order_detail.setVisibility(View.GONE);
		}
		// 价格类型
		spPriceTypeAdapter = ArrayAdapter.createFromResource(this,
				R.array.price_type, android.R.layout.simple_spinner_item);
		spPriceTypeAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_price_type.setAdapter(spPriceTypeAdapter);

		// 订单类型
		spOrderTypeAdapter = ArrayAdapter.createFromResource(this,
				R.array.order_type, android.R.layout.simple_spinner_item);
		spOrderTypeAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_order_type.setAdapter(spOrderTypeAdapter);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_btn_list:
			tv_btn_list.setVisibility(View.GONE);
			tv_btn_list.setEnabled(false);
			tv_btn_thumbnail.setVisibility(View.VISIBLE);
			tv_btn_thumbnail.setEnabled(true);
			lv_order_detail.setVisibility(View.VISIBLE);
			gv_order_detail.setVisibility(View.GONE);

			break;
		case R.id.tv_btn_thumbnail:
			tv_btn_list.setVisibility(View.VISIBLE);
			tv_btn_list.setEnabled(true);
			tv_btn_thumbnail.setVisibility(View.GONE);
			tv_btn_thumbnail.setEnabled(false);
			lv_order_detail.setVisibility(View.GONE);
			gv_order_detail.setVisibility(View.VISIBLE);
			break;
		case R.id.btn_search:
			Toast.makeText(this, "btn_search", Toast.LENGTH_SHORT).show();
			System.out.println("getPriceType = "
					+ searchEntities.getPriceType() + "\n getOrderType = "
					+ searchEntities.getOrderType() + "\n getInputKeyWord = "
					+ searchEntities.getInputKeyWord() + "\n getHotKeyword = "
					+ searchEntities.getHotKeyword());
			break;
		default:
			break;
		}

	}

	/**
	 * 点击下一页
	 */
	public void onNextPageClick() {
		//
		Toast.makeText(this, "onNextPageClick", Toast.LENGTH_SHORT).show();
	}

	/**
	 * 点击上一页
	 */
	public void onPrePageClick() {
		Toast.makeText(this, "onPrePageClick", Toast.LENGTH_SHORT).show();
	}

	/**
	 * 文字形式的主界面item事件处理
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long index) {
		Toast.makeText(PicViewActivity.this, "点击了第 " + position + " 个",
				Toast.LENGTH_SHORT).show();
	}

	private OnCheckedChangeListener listener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			switch (group.getCheckedRadioButtonId()) {
			case R.id.rb_underwear:
				Toast.makeText(PicViewActivity.this, rb_underwear.getText(),
						Toast.LENGTH_SHORT).show();
				searchEntities.setHotKeyword(rb_underwear.getText().toString());
				break;

			case R.id.rb_brassiere:
				Toast.makeText(PicViewActivity.this, rb_brassiere.getText(),
						Toast.LENGTH_SHORT).show();
				searchEntities.setHotKeyword(rb_brassiere.getText().toString());
				break;

			case R.id.rb_briefs:
				Toast.makeText(PicViewActivity.this, rb_briefs.getText(),
						Toast.LENGTH_SHORT).show();
				searchEntities.setHotKeyword(rb_briefs.getText().toString());
				break;

			default:
				break;
			}
		}
	};

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long arg3) {
		if (parent.getAdapter().equals(spOrderTypeAdapter)) {
			Toast.makeText(PicViewActivity.this,
					"sp_order_type " + spOrderTypeAdapter.getItem(position),
					Toast.LENGTH_SHORT).show();
			searchEntities.setOrderType(spOrderTypeAdapter.getItem(position)
					.toString());

		} else if (parent.getAdapter().equals(spPriceTypeAdapter)) {
			Toast.makeText(PicViewActivity.this,
					"sp_price_type " + spPriceTypeAdapter.getItem(position),
					Toast.LENGTH_SHORT).show();
			searchEntities.setPriceType(spPriceTypeAdapter.getItem(position)
					.toString());
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	
	
	
	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		searchEntities.setInputKeyWord(et_keyword.getText().toString());
	}
}
